#include<bits/stdc++.h>
#define int long long
using namespace std;
int q,l,r,dp[40][2][2];
inline int get(int x,int y){
	return (x>>y)&1;
}
inline int solve(int l,int r){
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=1;i++)
		for(int j=i;j<=1;j++){
			int f1=0,f2=0;
			if(i>=get(l,0)) f1=1;
			if(j<=get(r,0)) f2=1;
			dp[0][f1][f2]++;
		}
	for(int i=0;i<32;i++)
		for(int j=0;j<=1;j++)
			for(int k=0;k<=1;k++)
				for(int f1=0;f1<=1;f1++)
					for(int f2=f1;f2<=1;f2++){
						int nxt1=0,nxt2=0;
						if(f1>get(l,i+1)) nxt1=1;
						else if(f1==get(l,i+1)&&j) nxt1=1;
						if(f2<get(r,i+1)) nxt2=1;
						else if(f2==get(r,i+1)&&k) nxt2=1;
						dp[i+1][nxt1][nxt2]+=dp[i][j][k];
					}
	return dp[32][1][1];
}
signed main(){
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>q;
	while(q--){
		cin>>l>>r;
		cout<<solve(l,r)<<"\n";
	}
	return 0;
}